package io.renren.modules.medical.controller;

import io.renren.common.exception.MedicalException;
import io.renren.common.result.Result;
import io.renren.common.result.ResultCodeEnum;
import io.renren.common.utils.PageUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.medical.entity.VisitRecordEntity;
import io.renren.modules.medical.entity.VisitRecordStepEntity;
import io.renren.modules.medical.service.VisitRecordService;
import io.renren.modules.medical.service.VisitRecordStepService;
import io.renren.modules.medical.vo.medicalVo.VisitRecordSaveVo;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.Map;

/**
 * 医疗随访接口
 * @author luosheng
 * @email luoshengshuai@foxmail.com
 * @date 2023-03-31 10:14:39
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/medical/visit/record")
public class VisitRecordController {

    private final VisitRecordService visitRecordService;

    private final VisitRecordStepService visitRecordStepService;

    /**
     * 修改阶段数据
     */
    @RequiresPermissions("visitrestep:update")
    @PostMapping("updateStep")
    public Result<String> updateStep(@Validated(UpdateGroup.class) @RequestBody VisitRecordStepEntity visitRecordStepEntity){
        if (visitRecordStepEntity.getVisitRecordId() != null){
            throw new MedicalException(ResultCodeEnum.MODEL_INVALID_ERROR.getCode(), "修改时随访id只能为空");
        }
        if (visitRecordStepEntity.getReplyContent() != null){
            throw new MedicalException(ResultCodeEnum.MODEL_INVALID_ERROR.getCode(), "修改时回复内容只能为空");
        }
        visitRecordStepService.updateStep(visitRecordStepEntity);
        return Result.success(null);
    }

    /**
     * 详情
     */
    @RequiresPermissions("visitrecord:detail")
    @GetMapping("detail/{id}")
    public Result<VisitRecordEntity> getDetail(@PathVariable("id") Long id){
        VisitRecordEntity resultData = visitRecordService.getRecordDetail(id);
        return Result.success(resultData);
    }

    /**
     * 删除step表
     */
    @RequiresPermissions("visitrestep:delete")
    @PostMapping("removeStep/{stepId}")
    public Result<String> removeStep(@PathVariable("stepId") Long stepId){
        visitRecordStepService.removeStep(stepId);
        return Result.success(null);
    }

    /**
     * 新增阶段表数据
     */
    @RequiresPermissions("visitrestep:save")
    @PostMapping("saveStep")
    public Result<String> saveStep(@Validated(AddGroup.class) @RequestBody VisitRecordStepEntity visitRecordStepEntity){
        visitRecordService.saveStep(visitRecordStepEntity);
        return Result.success(null);
    }

    /**
     * 列表
     */
    @RequiresPermissions("visitrecord:list")
    @GetMapping("/list")
    public Result<PageUtils> list(@RequestParam Map<String, Object> params){
        PageUtils page = visitRecordService.queryPage(params);
        return Result.success(page);
    }

    /**
     * 信息
     */
    @RequiresPermissions("visitrecord:info")
    @GetMapping("/info/{id}")
    public Result<VisitRecordEntity> info(@PathVariable("id") Long id){
		VisitRecordEntity visitRecord = visitRecordService.getById(id);
        return Result.success(visitRecord);
    }

    /**
     * 保存
     */
    @RequiresPermissions("visitrecord:save")
    @PostMapping("/save")
    public Result<String> save(@Validated(AddGroup.class) @RequestBody VisitRecordSaveVo visitRecord){
		visitRecordService.saveVisitRecord(visitRecord);
        return Result.success(null);
    }

    /**
     * 修改
     */
    @RequiresPermissions("visitrecord:update")
    @PostMapping("/update")
    public Result<String> update(@Validated(UpdateGroup.class) @RequestBody VisitRecordEntity visitRecord){
		visitRecordService.updateById(visitRecord);
        return Result.success(null);
    }

    /**
     * 删除
     */
    @RequiresPermissions("visitrecord:delete")
    @PostMapping("/delete")
    public Result<String> delete(@RequestBody @NotNull Long[] ids){
		visitRecordService.removeVisitRecord(Arrays.asList(ids));
        return Result.success(null);
    }

}
